package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.message.UserWingRoomMessageData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/3/19 11:17
 */
public class WingRoomMessageBaseDaoImpl extends TempDataObjectDao<UserWingRoomMessageData> {

    public static String insertSql = "INSERT INTO t_u_wingroom_message_base(userId,todayMessageTimes,timesResetTime) values(?,?,?)";
    @Override
    protected boolean addData(UserWingRoomMessageData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTodayMessageTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesResetTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_message_base set todayMessageTimes=?,timesResetTime=? where userId=?";
    @Override
    protected boolean updateData(UserWingRoomMessageData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getTodayMessageTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomMessageData parseData(ResultSet rs) throws SQLException {
        UserWingRoomMessageData data = new UserWingRoomMessageData();
        data.setUserId(rs.getLong("userId"));
        data.setTodayMessageTimes(rs.getInt("todayMessageTimes"));
        data.setTimesResetTime(rs.getLong("timesResetTime"));
        return data;
    }

    public UserWingRoomMessageData getUserWingRoomMessageData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserWingRoomMessageData data = null;
        String sql = "select * from t_u_wingroom_message_base where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }
}
